package com.boesch.electroOffice.dataaccess.daoImpl.messerli;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.boesch.electroOffice.dataaccess.common.IllegalDataAccessException;
import com.boesch.electroOffice.dataaccess.common.ReturnCodesDataaccess;
import com.boesch.electroOffice.dataaccess.dao.messerli.ProjectDao;
import com.boesch.electroOffice.dataaccess.mapper.messerli.ProjectMesserliMapper;
import com.boesch.electroOffice.model.messerli.Project;

/**
 * This class executes the sql statements on the messerli database for class Project.
 * If a Exception is thrown, 
 * it will be unpacked to a illegalDataAccessException 
 * including a return code which can be shown to the user.
 * 
 * @author Ralph Koster
 * 
 */
@Repository
public class ProjectDaoImpl implements ProjectDao, Serializable {

	private static final long serialVersionUID = -7269392026782632448L;

	@Autowired
	private JdbcTemplate mfJdbcTemplate;

	@Value("${messerli.queryReadAllProjects}")
	private String queryReadAllProjects;

	/**
	 * {@inheritDoc}
	 */
	public List<Project> readAllProjects() throws IllegalDataAccessException {
		List<Project> projects = new ArrayList<Project>();
		try {
			projects = mfJdbcTemplate.query(queryReadAllProjects, new ProjectMesserliMapper());
			return projects;
		} catch (DataAccessException e) {
			throw new IllegalDataAccessException(ReturnCodesDataaccess.NO_OBJECTS_FOUND_ERROR.getValue(), e);
		}
	}
}
